﻿


var actionUrl = 'ashx/tenderhandler.ashx';
var formUrl = '/tenderinfo/html/tender.html';

$(function () {
    autoResize({ dataGrid: '#mGrid', gridType: 'datagrid', callback: mygrid.bindGrid, height: 5 });
    $('#a_add').click(crud.add);    
    $('#a_edit').click(crud.edit);
    $('#a_delete').click(crud.del);
});

var mygrid = {
    bindGrid: function (size) {
        $('#mGrid').datagrid({
            toolbar: '#toolbar',
            title: '招标列表',
            iconCls: 'icon icon-list',
            width: size.width,
            height: size.height,
            nowrap: false,
            rownumbers: true,
            animate: true,           
            url: actionUrl,            
            columns: [[
                { title: '招标编号', field: 'tbh', width: 200 },
                { title: '标名', field: 'tname', width: 60, align: 'center' },
                { title: '时间', field: 'tdate', width: 240 },
                { title: '单位', field: 'tcompany', width: 240 },
                {
                    title: '是否启用', field: 'isuse', width: 60, align: 'center', formatter: function (v, d, i) {
                        if (v == "1")
                            return '是';
                        else
                        return '否';
                    }
                }
            ]]
        });
    },
    reload: function () {
        $('#mGrid').datagrid('clearSelections').datagrid('reload');
    },
    selectRow: function () {
        return $('#mGrid').datagrid('getSelected');
    }
}

var crud = {
    createParam: function (action, keyid) {
        var o = {};
        var query = top.$('#myForm').serializeArray();
        query = convertArray(query);
        o.jsonEntity = JSON.stringify(query);
        o.action = action;
        o.keyid = keyid;
        return "json=" + JSON.stringify(o);
    },    
    add: function () {
        var addDialog = top.$.hDialog({
            href: formUrl + '?v=' + Math.random(), height: 300, title: "增加", iconCls: 'icon-add',
            submit: function () {
                if (top.$('#myForm').validate().form()) {
                    var query = crud.createParam('add', 0);
                    $.ajaxjson(actionUrl, query, function (d) {
                        if (d.Success) {
                            mygrid.reload();
                            addDialog.dialog('close');
                            msg.ok(d.Message);
                        } else {
                            MessageOrRedirect(d);
                        }
                    });
                }
                return false;
            }
        });       
    },
    edit: function () {
        var row = mygrid.selectRow();
        if (row) {
            var editDialog = top.$.hDialog({
                href: formUrl + '?v=' + Math.random(), height: 300, title: "修改", iconCls: 'icon-edit',
                onLoad: function() {
                    var viewModel = top.ko.mapping.fromJS(row);
                    top.ko.applyBindings(viewModel);
                },
                submit: function () {
                    if (top.$('#myForm').validate().form()) {                       
                        var query = crud.createParam('edit', row.KeyId);
                        $.ajaxjson(actionUrl, query, function (d) {
                            if (d.Success) {
                                mygrid.reload();
                                editDialog.dialog('close');
                                msg.ok(d.Message);
                            } else {
                                MessageOrRedirect(d);
                            }
                        });
                    }
                    return false;
                }
            });
        } else {
            msg.warning('请选择要修改的数据');
            return false;
        }
        return false;
    },
    del: function () {
        var row = mygrid.selectRow();
        if (row) {
                if (confirm('确认要删除此数据吗？')) {
                    $.ajaxjson(actionUrl, crud.createParam("del", row.KeyId), function (d) {
                        if (d.Success) {
                            mygrid.reload();
                            msg.ok(d.Message);
                        } else {
                            MessageOrRedirect(d);
                        }
                    });
                }
            
        }
        else {
            msg.warning('请选择要删除的数据.');
        }
        return false;
    }
}